// pages/login/index.js
import {
  wechat,
  user,
  wxapp
} from "../../api/apiData"
import Toast from '@vant/weapp/toast/toast';
let util = require('../../utils/util.js')


Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "",
    username: "",
    password: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.login({})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  setusername(e) {
    var val = e.detail.value
    this.setData({
      username: val
    })
  },
  setpassword(e) {

    var val = e.detail.value
    this.setData({
      password: val
    })
  },
  Login() {
    let {
      username,
      password
    } = this.data
    user.login({
      mobile: username,
      password:util.AesEncode(password)
    }, res => {
      let {
        code,
        msg,
        data
      } = res
      if (code == 1) {
        console.log(res)
        wx.setStorage({
          key: "token",
          data: data.token
        })
        wx.setStorage({
          key: "type",
          data: 'User'
        })
        wx.showToast({
          title: msg,
        })
        setTimeout(()=>{
          wx.navigateBack({
            delta: 0,
          })
        },1000)
      } else {
        wx.showToast({
          title: msg,
          icon:'error'
        })
      }

    })
  },
  VisiLogin() {
    let _this = this

    let iv, encryptedData

    wx.getUserProfile({
      desc: "个人信息",
      success: function (res) {

        iv = res.iv
        encryptedData = res.encryptedData
        Toast.loading()
        wx.login({
          success(res) {

            wechat.userLogin({
              code: res.code,
              iv,
              encryptedData
            }, res => {

              let {
                code,
                msg,
                data
              } = res
              console.log(res)
              Toast.clear()
              if (code == 0) {
                Toast(msg)
              } else {
                wx.setStorage({
                  key: "token",
                  data: data.token
                })
                wx.setStorage({
                  key: "type",
                  data: 'User'
                })
                // wx.switchTab({
                //   url: '/pages/index/index',
                // })
                wx.navigateBack({
                  delta: 0,
                })
              }

            })
          }
        })

      },
      fail(err) {


      }
    })


  },
  toResign() {
    wx.navigateTo({
      url: '/pages/resign/index',
    })
  },
  WxLogin() {
    wx.login({
      success(res) {
        let {
          code
        } = res
        wxapp.login({
          code
        }, res => {
          let {
            msg,
            data
          } = res
          if (res.code == 1) {
            console.log(res)
            wx.setStorage({
              key: "token",
              data: data.token
            })
            wx.setStorage({
              key: "type",
              data: 'User'
            })
            wx.showToast({
              title: msg,
            })
            setTimeout(()=>{
              wx.navigateBack({
                delta: 0,
              })
            },1000)
          } else {
            wx.showToast({
              title: msg,
              icon:'error'
            })
          }
        })

      }
    })

  }
})